<?php

$res = 0;
foreach ($object as $current) {
	if ($current['Receiptbook']['status'] == 'opened') {
		$res += $current['Receiptbook']['free_amount'];
	}
}

?>

<div id="add_form" class="largebloc2">
	<h3><?php echo __('add_receiptbook'); ?></h3>
	<table>
		<?php echo $form->create('Receiptbook', array('action' => 'index')); ?>
		<?php echo $form->hidden('Receiptbook.project_name', array ('value' => $project_name)); ?>
		<?php echo $form->hidden('Receiptbook.project_id', array ('value' => $project_id)); ?>
		<?php echo $form->hidden('Receiptbook.company_id', array ('value' => $company_id)); ?>
		<tr>
			<td>
				<?php echo $form->input('Receiptbook.name', array('error'=>'Please supply a name for this receiptbook.', 'label'=>__('name', true))); ?>
			</td>
		</tr>
		<tr>
			<td>
				<?php echo $form->input('Receiptbook.amount', array('error'=>'Please supply an amount for this receiptbook.', 'label'=>__('amount', true))); ?>
			</td>
		</tr>
		<tr>
			<td><?php echo $form->submit(__('validate', true)); ?></td>
			<td><?php echo $html->link(__('cancel', true), ''); ?></td>
		</tr>
		<?php echo $form->end(); ?>
	</table>
</div>
<br /><br />

<h2><?php echo __('opened_receiptbooks'); ?></h2>
<br />
Montant total restant : <?php echo $res; ?> days
<br /><br />
<table width="96%" border="0" cellspacing="0" cellpadding="0">
    <?php 
	echo $html->tableHeaders(array(__('receiptbook', true), __('creation_date', true), __('end_valid_date', true), __('amount', true), __('consumed', true), __('free_amount', true)), 'class="details"'); 
	foreach ($object as $current) {
		if (($current['Receiptbook']['project_id'] == $project_id) && ($current['Receiptbook']['status'] == 'opened')) {
			echo $html->tableCells(array($html->link($current['Receiptbook']['name'], '/' . $project_name . '/' . $project_id . '/receiptbooks/view/' . $current['Receiptbook']['id']),
							$current['Receiptbook']['creation_date'],
							$current['Receiptbook']['close_date'],
							$current['Receiptbook']['amount'],
							$current['Receiptbook']['amount'] - $current['Receiptbook']['free_amount'],
							$current['Receiptbook']['free_amount']),
						'class="odd"', 'class="even"');
	?>
			<?php if ($current['Funding']) { ?>
			<tr>
				<td colspan="6" style="padding-top:7px;padding-bottom:7px;">
					<table width="96%" border="0" cellspacing="0" cellpadding="0">
                        <?php 
						echo $html->tableHeaders(array(__('ticket', true), __('kind', true), __('state', true), __('milestone', true), __('charge', true)), 'class="details"');
						foreach ($current['Funding'] as $currentFunding) { 
							echo $html->tableCells(array($html->link($currentFunding['Ticket']['name'], '/' . $project_name . '/'. $project_id . '/tickets/view/' . $currentFunding['Ticket']['id']),
											$currentFunding['Ticket']['kind'],
											$currentFunding['Ticket']['status'],
											$currentFunding['Ticket']['Milestone']?$currentFunding['Ticket']['Milestone']['name']:'',
											$currentFunding['amount']),
										'class="odd"', 'class="even"');
						
						} 
						?>
					</table>
				</td>
			</tr>
			<?php } ?>
		<?php } ?>
	<?php } ?>
</table>

<br /><br />

<h2><?php echo __('closed_receiptbooks'); ?></h2>
<br />
<table width="96%" border="0" cellspacing="0" cellpadding="0">
    <?php 
	echo $html->tableHeaders(array(__('receiptbook', true), __('creation_date', true), __('end_valid_date', true), __('amount', true)), 'class="details"'); 
	foreach ($object as $current) {
		if (($current['Receiptbook']['project_id'] == $project_id) && ($current['Receiptbook']['status'] == 'closed')) {
			echo $html->tableCells(array($html->link($current['Receiptbook']['name'], '/' . $project_name . '/' . $project_id . '/receiptbooks/view/' . $current['Receiptbook']['id']),
							$current['Receiptbook']['creation_date'],
							$current['Receiptbook']['close_date'],
							$current['Receiptbook']['amount']),
						'class="odd"', 'class="even"');
	?>
			<?php if ($current['Funding']) { ?>
			<tr>
				<td colspan="6" style="padding-top:7px;padding-bottom:7px;">
					<table width="96%" border="0" cellspacing="0" cellpadding="0">
                        <?php 
						echo $html->tableHeaders(array(__('ticket', true), __('kind', true), __('charge', true)), 'class="details"');
						foreach ($current['Funding'] as $currentFunding) { 
							echo $html->tableCells(array($html->link($currentFunding['Ticket']['name'], '/' . $project_name . '/'. $project_id . '/tickets/view/' . $currentFunding['Ticket']['id']),
											$currentFunding['Ticket']['kind'],
											$currentFunding['amount']),
										'class="odd"', 'class="even"');
						
						} 
						?>
					</table>
				</td>
			</tr>
			<?php } ?>
		<?php } ?>
	<?php } ?>
</table>